class Solution
{
public:
    bool canFormArray(vector<int> &arr, vector<vector<int>> &pieces)
    {
        int n = arr.size();
        for (int i = 0; i < n;)
        {
            bool foundFirst = false;
            for (auto &piece : pieces)
            {
                if (piece[0] == arr[i])
                {
                    for (int j = 0; j < piece.size();)
                    {
                        if (piece[j] != arr[i])
                        {
                            return false;
                        }
                        ++j;
                        ++i;
                    }
                    foundFirst = true;
                    break;
                }
            }
            if (!foundFirst)
            {
                return false;
            }
        }
        return true;
    }
};